﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evolution
{
    class Program
    {
        const int n = 30;
        static int[,] myarr = new int[n,n];
        static int[,] myarr2 = new int[n, n];
 

        static void ShowMas()           //displays the array on the screen
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                    if (myarr[i, j] == 0) Console.Write(' ');
                    else Console.Write('*');
                Console.WriteLine();
            }
        }

        static void SpacePress()        //if the space is active
        {
            int counter=0;
            for (int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    switch(myarr[i,j])
                    {
                        case 0:             //if the cell is empty
                            for (int k = i - 1; k <= i + 1; k++)
                            {
                                if ((k == -1) || (k == n)) continue;    //because there is no element
                                for (int l = j - 1; l <= j + 1; l++)
                                {
                                    if ((l == -1) || (l == n)) continue;//because there is no element 
                                    if (myarr[k, l] == 1) counter++;
                                }
                            }
                            if (counter >= 2) myarr2[i, j] = 1;
                            else myarr2[i, j] = 0;
                            counter = 0;
                                break;
                        case 1:          //if the cell is not empty
                            for (int k = i - 1; k <= i + 1; k++)
                            {
                                if ((k == -1) || (k == n)) continue;    //because there is no element
                                for (int l = j - 1; l <= j + 1; l++)
                                {
                                    if ((l == -1) || (l == n)) continue;//because there is no element
                                    if (myarr[k, l] == 0) counter++;
                                }
                            }
                            if (counter == 0) myarr2[i, j] = 0;
                            else myarr2[i, j] = 1;
                            counter = 0;
                            break;
                    }
                }
            
        }




        static void Main(string[] args)
        {
            Random rnd = new Random();
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    myarr[i, j] = rnd.Next(0, 2);       //random number generation


            Console.Clear();
            Console.WindowHeight = 32;
            
            ShowMas();
            ConsoleKey key;
            
            int[,] temp;
            Console.WriteLine("Press ESC to exit");
            while ((key=Console.ReadKey(true).Key) != ConsoleKey.Escape)    //Esc - is the exit
            {
                if (key == ConsoleKey.Spacebar)
                {
                    SpacePress();
                    temp = myarr;       //exchange value of arrays
                    myarr = myarr2;
                    myarr2 = temp;
                    Console.Clear();
                    ShowMas();
                    Console.WriteLine("Press ESC to exit");

                };
            }
        }
    }
}
